﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.IO;
using System.Data;
using SDD;
using NPOI.HSSF.UserModel;

namespace SDD
{
    class StrongList
    {
        public static string dataDay = Program.DataDay;

        public string url1 = "http://justdata.yuanta.com.tw/z/zk/zkf/zkResult.asp?D=0&A=x@10,a@22;x@30,a@5;x@90,a@2,b@6;x@270,a@1000;x@1000,a@75;x@1040&site=";
        //http://justdata.yuanta.com.tw/z/zk/zkf/zkResult.asp?D=0&A=x@10,a@22;x@30,a@5;x@90,a@2,b@6;x@270,a@1000;x@1000,a@75;x@1040&site=


        private string FullFileName = Program.FileDir + "強勢選股.xls";
        public string msg = "";

        public void AggregateData()
        {
            // 期交所
            try
            {
                //Debug.Write("dataDay:" + dataDay);
                //analysisHTML(url1);
                new UpdateData().UpdateDataToGoogleDoc(FullFileName);
            }
            catch (Exception e)
            {
                Console.WriteLine("強勢選股 資料擷取失敗!! " + e.Message);
            }
          
        }
        /// <summary>
        /// 解析HTML new
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ds"></param>
        public void analysisHTML(string url)
        {

            string context = new cHttp().dopost(url);
            WebBrowser wbr = new WebBrowser();
            wbr.Navigate("");

            wbr.ScriptErrorsSuppressed = true;
            wbr.Document.Write("<script type='text/javascript'>window.onerror = function(){};</script>" + context);

            HtmlElementCollection tb = wbr.Document.GetElementsByTagName("TABLE");
            //dataDay = tb[0].GetElementsByTagName("SPAN")[1].InnerHtml.Replace("日期", "");
            HtmlElementCollection tr = tb[1].GetElementsByTagName("TR");

            /////////////////////
            //建立工作表
            NPOIComFunction xls = new NPOIComFunction(FullFileName);
            HSSFSheet sheet;
            // 依年月分頁儲存
            string sheetName = dataDay;
            if (xls.Workbook.GetSheetIndex(dataDay) > -1)
            {
                Console.WriteLine("強勢選股 " + dataDay + " 資料已存在!!!");
                return;
            }
            sheet = xls.Workbook.CreateSheet(dataDay);

            // 開始寫資料
            try
            {
                for (int i = 0; i < tr.Count; i++)
                {
                    int max_j = tr[i].Children.Count;
                  
                    int inRecCnt = 0;
                    string SIDName = tr[i].Children[0].InnerText;
                    bool IsContinue = true;
                    //迴圈跑是否持續進榜
                    for (int befDayCnt = 1; befDayCnt < xls.Workbook.NumberOfSheets; befDayCnt++)
                    {
                        // befDayCnt 要往前推算幾天
                        //1.先取得工作表名稱
                        try
                        {
                            HSSFSheet befSheet = xls.Workbook.GetSheetAt(xls.Workbook.GetSheetIndex(dataDay) - befDayCnt);
                            IsContinue = false;
                            //2.跑工作表迴圈
                            //befSheet.LastRowNum應該為2~101
                            for (int befSheetRowCnt = 1; befSheetRowCnt <= befSheet.LastRowNum; befSheetRowCnt++)
                            {
                                //IDNAME 在第0列
                                if (befSheet.GetRow(befSheetRowCnt).GetCell(0).StringCellValue.Trim() == SIDName.Trim())
                                {
                                    IsContinue = true;
                                    inRecCnt++;
                                    break; //離開befSheet
                                }
                            }
                            if (IsContinue == false)
                                break; //離開計算持續進榜
                        }
                        catch { }
                    }
                    //寫入次數
                  
                    if (inRecCnt == 0)
                    {
                        for (int j = 0; j < tr[i].Children.Count; j++)
                        {
                            sheet.CreateRow(i).CreateCell(j).SetCellValue(tr[i].Children[j].InnerText);
                        }
                        sheet.CreateRow(i).CreateCell(tr[i].Children.Count ).SetCellValue("新進榜");
                    }
                    else //寫入次數並上色
                    {

                        //設定不同次數的顏色
                        HSSFCellStyle style1 = xls.Workbook.CreateCellStyle();
                        if (inRecCnt == 1)
                            style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LIGHT_YELLOW.index;
                        else if (inRecCnt >= 2 && inRecCnt <= 5)
                            style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.TAN.index;
                        else if (inRecCnt > 5)
                            style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.ROSE.index;
                        style1.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;

                        for (int j = 0; j < tr[i].Children.Count; j++)
                        {
                            HSSFCell cell = sheet.CreateRow(i).CreateCell(j);
                            cell.CellStyle = style1;
                            cell.SetCellValue(tr[i].Children[j].InnerText);
                        }
                        HSSFCell cell_final = sheet.CreateRow(i).CreateCell(tr[i].Children.Count);
                        cell_final.CellStyle = style1;
                        cell_final.SetCellValue("持續進榜[" + inRecCnt.ToString() + "]");



                    }//寫入次數 end

                } // i 結束
            }
            catch
            {

            }

            /****/
            //讓目前的工作表選為預設
            xls.Workbook.ActiveSheetIndex = xls.Workbook.NumberOfSheets - 1;
            xls.Workbook.SetSelectedTab(xls.Workbook.NumberOfSheets - 1);
            sheet.IsSelected = true;

            // 寫入實體檔案
            xls.WriteToFile();


        }
    }
}
